#include "irq.h"
#include "time.h"
#include "sched.h"
#include "string.h"
#include "screen.h"
#include "mac.h"

/* exception handler */
uint64_t exception_handler[32];
//uint64_t cost;
/* used to init PCB */
uint32_t initial_cp0_status;

// extern void do_shell();

static void irq_timer()
{
    screen_reflush();
    //current_running->cursor_x = screen_cursor_x;
    //current_running->cursor_y = screen_cursor_y;
    /* increase global time counter */
    time_elapsed += TIMER_INTERVAL;
    /* reset timer register */
    reset_timer(TIMER_INTERVAL);
    //cost = get_cp0_count()/1000; 
    /* sched.c to do scheduler */
    //current_running_state = 1;
   
    do_scheduler();
    //cost = get_cp0_count()/1000 - cost;
    //vt100_move_cursor(1, 1);
    //printk(" > [COOOOOOOST] do scheduler cost %d us", cost);
    //time_elapsed += get_cp0_count();
    }
void other_exception_handler(uint32_t cause,uint64_t epc) 
{
    time_elapsed += get_cp0_count();
    //kprintf("cause: %x, epc: %x\n",cause,epc);
    reset_timer(TIMER_INTERVAL);
}
void interrupt_helper(uint32_t status, uint32_t cause)
{
    uint32_t sign = status & cause ;
    sign &= 0x0000ff00;

    volatile uint32_t *intisr;
    intisr = (uint32_t *)0xffffffffbfe11420;

    if(sign == 0x00008000)
        irq_timer();
    else if((sign == 0x00001000) && ((*intisr & (1 << 12)) != 0))
    {
        kprintf("mac int\n");
        //mac_irq_handle();
    }
    else
        other_exception_handler(status, (uint64_t)cause);
}

